package com.yc.upload;

import com.yc.upload.vo.UploadProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
@ComponentScan
@ConditionalOnProperty(prefix = "upload", value = "enable")
public class UploadAutoConfiguration implements WebMvcConfigurer {

    @Resource
    UploadProperties properties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        properties.getMapping().forEach((k, v) -> {
            registry.addResourceHandler("/"+k+"/**")
                    .addResourceLocations("file:"+v);
        });
    }
}
